/*
 Copyright (C) 2010 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.topisv.api.response;

import org.dom4j.Element;
import org.dom4j.Node;
import org.qdss.topisv.api.Result;

/**
 * 接口调用失败的错误解析
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">Zhao Fangfang</a>
 * @since 0.2, 2010-8-27
 * @version $Id: ErrorRspParser.java 74 2013-06-28 07:53:31Z zhaofang123 $
 */
public class ErrorRspParser {
	
	/**
	 * 封装调用失败对象
	 * 
	 * @param rootElement
	 * @return
	 */
	public static ErrorRsp parse(Element rootElement) {
		Node aNode = rootElement.selectSingleNode("/error_response/code");
		if (aNode == null) {  // No error
			return null;
		}
		
		final String code = aNode.getText();
		aNode = rootElement.selectSingleNode("//msg");
		final String msg = (aNode == null) ? null : aNode.getText();
		aNode = rootElement.selectSingleNode("//sub_code");
		final String sub_code = (aNode == null) ? null : aNode.getText();
		aNode = rootElement.selectSingleNode("//sub_msg");
		final String sub_msg = (aNode == null) ? null : aNode.getText();
		
		aNode = rootElement.selectSingleNode("//args/arg/key[text()='method']");
		if (aNode != null) {
			aNode = aNode.getParent().selectSingleNode("value");
		}
		return (new ErrorRsp((aNode == null) ? "Unknow" : aNode.getText(), code, msg, sub_code, sub_msg));
	}
	
	/**
	 * 如果调用失败，则抛出异常
	 * 
	 * @param result
	 * @throws IllegalResultException
	 * @throws UnauthorizedException
	 * @throws OutOfFrequencyLimitException
	 * @throws ErrorRspException
	 */
	public static void tryIfError(Result result) 
			throws UnauthorizedException, OutOfFrequencyLimitException, ErrorRspException {
		tryIfError(result.rootElement());
	}
	
	/**
	 * 如果调用失败，则抛出异常
	 * 
	 * @param rootElement
	 * @throws UnauthorizedException
	 * @throws OutOfFrequencyLimitException
	 * @throws ErrorRspException
	 */
	public static void tryIfError(Element rootElement)
			throws UnauthorizedException, OutOfFrequencyLimitException, ErrorRspException {
		ErrorRsp rsp = parse(rootElement);
		if (rsp == null) {
			return;
		}
		
		final String R_MSG = rsp.toString();
		final int R_CODE = rsp.getCode();
		if (R_CODE == 25 || R_CODE == 27 
				|| R_MSG.contains("unmix-sessionkey-failure")/* 无效session，例如：abc */ ) {
			throw new UnauthorizedException("授权错误: " + R_MSG);
		} else if (R_CODE == 11/* IP受限 */
				|| R_MSG.contains("security authorize invalid") /* R/W等级受限 */) {
			throw new SecurityLimitException("安全等级不允许: " + R_MSG);
		} else if (R_CODE >= 4 && R_CODE <= 8) {
			throw new OutOfFrequencyLimitException("调用超限: " + R_MSG);
		}
		throw new ErrorRspException(rsp);
	}
}
